﻿using B.S.BasicData.Infrastructure.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace B.S.BasicData.Infrastructure.Implement
{
    public class BaseRepository<T>:IBaseRepository<T>where T : class
    {
        MyDbContext db;

        public MyDbContext Context { get; set; }
        public BaseRepository(MyDbContext db, MyDbContext context)
        {
            this.db = db;
            Context = context;
        }

        /// <summary>
        /// 新增方法
        /// </summary>
        /// <param name="entity">需要增加的数据</param>
        /// <returns>新增行数</returns>
        public async Task<int> CreateAsync(T entity)
        {
            try
            {
                db.Set<T>().Add(entity);
                return await db.SaveChangesAsync();
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 新增批量方法
        /// </summary>
        /// <param name="entity">需要增加的数据</param>
        /// <returns>新增行数</returns>
        public async Task<int> CreateAsync(List<T> entity)
        {
            try
            {
                db.Set<T>().AddRange(entity);
                return await db.SaveChangesAsync();
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 修改方法
        /// </summary>
        /// <param name="entity">需要修改的数据</param>
        /// <returns>修改行数</returns>
        public async Task<int> UpdateAsync(T entity)
        {
            try
            {
                db.Set<T>().Update(entity);
                return await db.SaveChangesAsync();
            }
            catch (Exception)
            {

                throw;
            }
        }


        /// <summary>
        /// 修改批量方法
        /// </summary>
        /// <param name="entity">需要修改的数据</param>
        /// <returns>修改行数</returns>
        public async Task<int> UpdateAsync(List<T> entity)
        {
            try
            {
                db.Set<T>().UpdateRange(entity);
                return await db.SaveChangesAsync();
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 根据Id进行单条件查询
        /// </summary>
        /// <param name="Id">实体Id</param>
        /// <returns>查询数据</returns>
        public async Task<T> GetValueAsync(int id)
        {
            try
            {
                return await db.Set<T>().FindAsync(id);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 搜索数据信息
        /// </summary>
        /// <returns>数据列表</returns>
        public IQueryable<T> GetAll()
        {
            try
            {
                return db.Set<T>().AsQueryable();
            }
            catch (Exception)
            {

                throw;
            }
        }

    }
}
